﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;

namespace Catering.Catering_Page.Caipin.Caipin_jcsz
{
    /// <summary>
    /// Json序列化和反序列化的帮助方法
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// JSON序列化：把对象序列化成Json格式的字符串
        /// </summary>
        public static string Serialize<T>(T data)
        {
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType());

            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, data);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }


        /// <summary>
        /// JSON反序列化：根据Json格式的字符串，反序列化成对象
        /// </summary>
        public static T Deserialize<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
        }

        /// <summary>
        /// 功能简介：由数据表得出关联的Json对象的数组,并返回
        /// 创建人：PengChenWang
        /// 创建时间：2013-4-16
        /// </summary>
        /// <param name="sourceDT"></param>
        /// <param name="columnNameArray"></param>
        /// <returns></returns>
        public static string GetJsonData(DataTable sourceDT, params string[] columnNameArray)
        {
            StringBuilder sbJson = new StringBuilder("");
            object obj = null;
            if (sourceDT == null || sourceDT.Rows.Count == 0 || columnNameArray == null)
                return "[]";
            sbJson.Append("[");
            for (int iRow = 0; iRow < sourceDT.Rows.Count; iRow++)
            {
                if (iRow > 0)
                    sbJson.Append(",");
                sbJson.Append("{");
                for (int iColumn = 0; iColumn < columnNameArray.Length; iColumn++)
                {
                    if (iColumn > 0)
                        sbJson.Append(",");
                    if (!sourceDT.Columns.Contains(columnNameArray[iColumn]))
                        continue;
                    obj = sourceDT.Rows[iRow][columnNameArray[iColumn]];
                    sbJson.AppendFormat("\"{0}\":\"{1}\"", columnNameArray[iColumn], obj == null ? "" : obj.ToString().Replace("\r","").Replace("\n",""));
                }
                sbJson.Append("}\r\n");
            }
            sbJson.Append("];");
            return sbJson.ToString();
        }
    }
}
